package com.whately.core.redis.utils;

import com.whately.core.base.model.RedisKeyBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisCacheListUtils {

    private final Logger logger = LoggerFactory.getLogger(RedisCacheListUtils.class);

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**************************************************************************
     *     list
     *************************************************************************/
    public List<String> range(RedisKeyBuilder key, long start, long end) {
        return redisTemplate.opsForList().range(key.getKey(), start, end);
    }

    public void trim(RedisKeyBuilder key, long start, long end) {
        redisTemplate.opsForList().trim(key.getKey(), start, end);
    }

    public Long size(RedisKeyBuilder key) {
        return redisTemplate.opsForList().size(key.getKey());
    }

    public Long leftPush(RedisKeyBuilder key, String value) {
        return redisTemplate.opsForList().leftPush(key.getKey(),value);
    }

    public Long leftPushAll(RedisKeyBuilder key, String... values) {
        return redisTemplate.opsForList().leftPushAll(key.getKey(),values);
    }

    /**
     * Insert all {@literal values} at the head of the list stored at
     * {@literal key}.
     *
     * @param key    must not be {@literal null}.
     * @param values must not be {@literal empty} nor contain {@literal null}
     *               values.
     * @return
     * @since 1.5
     */

    public Long leftPushAll(RedisKeyBuilder key, Collection<String> values) {
        return redisTemplate.opsForList().leftPushAll(key.getKey(),values);

    }

    public Long leftPushIfPresent(RedisKeyBuilder key, String value) {
        return redisTemplate.opsForList().leftPushIfPresent(key.getKey(),value);

    }


    public Long rightPush(RedisKeyBuilder key, String value) {
        return redisTemplate.opsForList().rightPush(key.getKey(), value);

    }

    public Long rightPushAll(RedisKeyBuilder key, String... values) {
        return redisTemplate.opsForList().rightPushAll(key.getKey(),values);

    }

    /**
     * Insert all {@literal values} at the tail of the list stored at
     * {@literal key}.
     *
     * @param key    must not be {@literal null}.
     * @param values must not be {@literal empty} nor contain {@literal null}
     *               values.
     * @return
     * @since 1.5
     */

    public Long rightPushAll(RedisKeyBuilder key, Collection<String> values) {
        return redisTemplate.opsForList().rightPushAll(key.getKey(),values);

    }

    public Long rightPushIfPresent(RedisKeyBuilder key, String value) {
        return redisTemplate.opsForList().rightPushIfPresent(key.getKey(),value);
    }


    public void set(RedisKeyBuilder key, long index, String value) {
        redisTemplate.opsForList().set(key.getKey(),index,value);
    }

    public Long remove(RedisKeyBuilder key, long i, Object value) {
        return redisTemplate.opsForList().remove(key.getKey(), i, value);
    }

    public String index(RedisKeyBuilder key, long index) {
        return redisTemplate.opsForList().index(key.getKey(), index);
    }

    public String leftPop(RedisKeyBuilder key) {
        return redisTemplate.opsForList().leftPop(key.getKey());

    }

    public String leftPop(RedisKeyBuilder key, long timeout, TimeUnit unit) {
        return redisTemplate.opsForList().leftPop(key.getKey(), timeout, unit);
    }

    public String rightPop(RedisKeyBuilder key) {
        return redisTemplate.opsForList().rightPop(key.getKey());

    }

    public String rightPop(RedisKeyBuilder key, long timeout, TimeUnit unit) {
        return redisTemplate.opsForList().rightPop(key.getKey(), timeout, unit);
    }

    public String rightPopAndLeftPush(RedisKeyBuilder sourceKey, RedisKeyBuilder destinationKey) {
        return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey.getKey(), destinationKey.getKey());
    }

    public String rightPopAndLeftPush(RedisKeyBuilder sourceKey, RedisKeyBuilder destinationKey, long timeout, TimeUnit unit) {
        return redisTemplate.opsForList().rightPopAndLeftPush(sourceKey.getKey(), destinationKey.getKey(), timeout, unit);
    }

}
